import 'dart:ui';

import 'package:fate_blind_box/im/persondetail.dart';
import 'package:fate_blind_box/network/Api.dart';
import 'package:fate_blind_box/routes/page_route_names.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/snack_bar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../widget/photo_view.dart';

class PersonInfo extends StatefulWidget {
  String? sig;

  PersonInfo({this.sig, Key? key}) : super(key: key);

  @override
  State<PersonInfo> createState() => _PersonInfoState();
}

class _PersonInfoState extends State<PersonInfo> {
  Map _userInfo = {};

  @override
  void initState() {
    super.initState();
    _getMemberInfo();
  }

  _getMemberInfo() async {
    final res = await Api.getInstance().getFriendInfo(widget.sig ?? '');
    if (res['code'] == 0) {
      final data = res['data'];
      if (data != null) {
        setState(() {
          _userInfo = data;
        });
      } else {
        ThemeSnackBar.show(context, "用户信息错误");
      }
    } else {
      ThemeSnackBar.show(context, res['msg']);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffEDEDED),
      body: Column(
        children: [
          Expanded(
              child: SingleChildScrollView(
            child: Column(
              children: [
                _buildHead(),
                // _buildCent(),
                // _buildImG(),
              ],
            ),
          )),
          Container(
            color: Colors.white,
            height: 100,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: GestureDetector(
                onTap: () {
                  if (_userInfo['easeuserId'] != null) {
                    if (Get.previousRoute != PageRouteNames.messageChat) {
                      Get.toNamed(PageRouteNames.messageChat, arguments: {
                        'nickName': '${_userInfo['nickName']}',
                        'id': '${_userInfo['easeuserId']}',
                        'avatarUrl': _userInfo['profilePhoto'],
                      });
                      return;
                    }
                    Get.back();
                  }
                },
                child: Container(
                  margin: const EdgeInsets.only(
                      bottom: 30, top: 0, left: 50, right: 50),
                  height: 40,
                  // width: 200,
                  alignment: Alignment.center,
                  decoration: ShapeDecoration(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20)),
                    color: ThemeColor.colorCF8F23,
                  ),
                  // child: const Text("私聊", style: TextStyle(color: Colors.white),),
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          "images/ic_msg.png",
                          color: Colors.white,
                          width: 16,
                          height: 16,
                        ),
                        const SizedBox(
                          width: 3,
                        ),
                        const Text(
                          "发消息",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                              fontWeight: FontWeight.normal),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildImG() {
    final size = MediaQuery.of(context).size;
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.only(left: 19, right: 19),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(right: 30),
                      child: Text(
                        "照片墙",
                        style: TextStyle(
                            color: ThemeColor.textColor,
                            fontSize: 16,
                            fontWeight: FontWeight.normal),
                      ),
                    ),
                    _buildListView(),
                  ],
                ),
                Icon(
                  Icons.navigate_next,
                  color: ThemeColor.textColorSecond,
                  size: 20,
                ),
              ],
            ),
          ),
          Divider(
            indent: 0.0,
            endIndent: 0.0,
            height: 1,
            thickness: 1,
            color: Colors.grey[200],
          ),
          Container(
            color: Colors.white,
            margin:
                const EdgeInsets.only(top: 16, bottom: 20, left: 16, right: 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "更多信息",
                  style: TextStyle(
                      color: ThemeColor.textColor,
                      fontSize: 16,
                      fontWeight: FontWeight.normal),
                ),
                Icon(
                  Icons.navigate_next,
                  color: ThemeColor.textColorSecond,
                  size: 20,
                ),
              ],
            ),
          ),
          Divider(
            height: 1,
            thickness: 9,
            color: Colors.grey[200],
          ),
        ],
      ),
    );
  }

  /// 列表
  Widget _buildListView() {
    List<String> imgList = _userInfo?["memberAvatarList"] ?? [];

    // final avatar = _userInfo?["profilePhoto"];
    final photoWallList = _userInfo?["photoWall"];
    if (imgList.isEmpty &&
        photoWallList != null &&
        photoWallList.toString().isNotEmpty) {
      for (var item in _userInfo?["photoWall"]) {
        imgList.add(item);
      }
    }
    return Container(
      height: 60,
      margin: const EdgeInsets.only(bottom: 16, top: 16),
      child: ListView.builder(
        itemCount: imgList.length,
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            margin: const EdgeInsets.only(right: 4),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: Image.network(
                imgList[index],
                width: 60,
                height: 60,
                fit: BoxFit.cover,
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildCent() {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "备注和标签",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       Icon(
          //         Icons.navigate_next,
          //         color: ThemeColor.textColorSecond,
          //         size: 22,
          //       ),
          //     ],
          //   ),
          // ),
          // Divider(
          //   indent: 0.0,
          //   endIndent: 0.0,
          //   height: 9,
          //   thickness: 1,
          //   color: Colors.grey[200],
          // ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, left: 19, right: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "朋友权限",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       Icon(
          //         Icons.navigate_next,
          //         color: ThemeColor.textColorSecond,
          //         size: 20,
          //       ),
          //     ],
          //   ),
          // ),
          // Divider(
          //   height: 9,
          //   thickness: 9,
          //   color: Colors.grey[200],
          // ),
          // Container(
          //   margin:
          //       const EdgeInsets.only(top: 16, bottom: 14, right: 19, left: 19),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Text(
          //         "社交圈",
          //         style: TextStyle(
          //             color: ThemeColor.textColor,
          //             fontSize: 16,
          //             fontWeight: FontWeight.normal),
          //       ),
          //       Icon(
          //         Icons.navigate_next,
          //         color: ThemeColor.textColorSecond,
          //         size: 20,
          //       ),
          //     ],
          //   ),
          // ),
          // Divider(
          //   indent: 0,
          //   endIndent: 0,
          //   height: 1,
          //   thickness: 1,
          //   color: Colors.grey[200],
          // ),
        ],
      ),
    );
  }

  Widget _buildHead() {
    return Column(
      children: [
        Stack(
          children: [
            _userInfo['profilePhoto'] != null
                ? Positioned.fill(
                    child: Image.network(
                      _userInfo['profilePhoto'] ?? '',
                      width: 71,
                      height: 71,
                      fit: BoxFit.cover,
                    ),
                  )
                : Container(),
            BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
              child: Container(
                height: 200,
                margin: const EdgeInsets.all(0),
                padding: const EdgeInsets.all(0),
                decoration: const BoxDecoration(
                  color: Color.fromRGBO(127, 105, 217, 0.02),
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  child: Container(
                    padding: const EdgeInsets.fromLTRB(20, 55, 0, 0),
                    child: const Icon(
                      Icons.arrow_back_ios,
                      size: 22,
                      color: Colors.white,
                    ),
                  ),
                  onTap: () {
                    Navigator.pop(context);
                  },
                ),
                GestureDetector(
                    onTap: () {
                      if (_userInfo.isEmpty) return;
                      Get.to(PersonDatail(userInfo: _userInfo),
                          routeName: PageRouteNames.personDetailPage);
                    },
                    child: Container(
                      padding: const EdgeInsets.fromLTRB(0, 55, 20, 0),
                      child: const Icon(
                        Icons.more_horiz,
                        size: 22,
                        color: Colors.white,
                      ),
                    )),
              ],
            )
          ],
        ),
        ClipRRect(
          child: Container(
            color: Colors.white,
            padding: const EdgeInsets.fromLTRB(19, 30, 0, 20),
            child: Row(
              children: [
                _userInfo['profilePhoto'] != null
                    ? GestureDetector(
                        onTap: () {
                          Get.to(PhotoPreview(
                            galleryItems: [_userInfo['profilePhoto']],
                            defaultImage: 0,
                          ));
                        },
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(5),
                          child: Image.network(
                            _userInfo['profilePhoto'],
                            width: 71,
                            height: 71,
                            fit: BoxFit.cover,
                          ),
                        ),
                      )
                    : Container(),
                const SizedBox(
                  width: 12,
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.only(left: 0, bottom: 3),
                      width: 150, //
                      child: Text(
                        _userInfo['nickName'] ?? "",
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: ThemeColor.textColor,
                          fontSize: 16,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                    ),
                    Container(
                      padding:
                          const EdgeInsets.only(left: 0, bottom: 3, top: 10),
                      width: 150,
                      child: Text(
                        "都肆ID:" + _userInfo['profileNo'].toString(),
                        style: TextStyle(
                            color: ThemeColor.textColorSecond,
                            fontSize: 10,
                            fontWeight: FontWeight.normal),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    // Row(
                    //   crossAxisAlignment: CrossAxisAlignment.center,
                    //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    //   children: [
                    //     Container(
                    //       // padding: const EdgeInsets.only(left: 5,bottom: 3),
                    //       alignment: Alignment.center,
                    //       decoration: ShapeDecoration(
                    //           shape: RoundedRectangleBorder(
                    //             borderRadius: BorderRadius.circular(8),
                    //           ),
                    //           color: _userInfo['realSex'] == 2
                    //               ? ThemeColor.mainColor
                    //               : Colors.blue),
                    //       width: 40,
                    //       height: 16,
                    //       child: IconText(
                    //         _userInfo['age'].toString() ?? '',
                    //         icon: Icon(
                    //             _userInfo['realSex'] == 2
                    //                 ? Icons.female
                    //                 : Icons.male,
                    //             color: Colors.white),
                    //         iconSize: 12,
                    //         style: const TextStyle(
                    //           color: Colors.white,
                    //           fontSize: 12,
                    //         ),
                    //       ),
                    //     ),
                    //     Padding(
                    //       padding: const EdgeInsets.only(
                    //         left: 5,
                    //       ),
                    //       child: Image.asset(
                    //         "images/ic_path.png",
                    //         width: 11,
                    //         height: 13,
                    //       ),
                    //     ),
                    //     Padding(
                    //       padding: const EdgeInsets.only(left: 3),
                    //       child: Text(
                    //         '${_userInfo['homeAddr'] ?? ''}',
                    //         // '北京 市朝阳区/女♀/24岁',
                    //         style: const TextStyle(
                    //             fontSize: 12, color: Colors.grey),
                    //       ),
                    //     ),
                    //   ],
                    // )
                  ],
                ),
              ],
            ),
          ),
        )
      ],
    );
  }
}
